<template>
  <div class="app-container">
    <el-form :inline="true" :model="queryParams" label-width="68px" ref="queryForm" size="small" v-show="showSearch">
      <el-form-item label="spu编码" prop="spuCode">
        <el-input
          @keyup.enter.native="handleQuery"
          clearable
          placeholder="请输入spu编码"
          v-model="queryParams.spuCode"
        />
      </el-form-item>
      <el-form-item label="spu名字" prop="name">
        <el-input
          @keyup.enter.native="handleQuery"
          clearable
          placeholder="请输入spu名字"
          v-model="queryParams.name"
        />
      </el-form-item>
      <el-form-item label="一级分类ID" prop="categoryFirst">
        <el-input
          @keyup.enter.native="handleQuery"
          clearable
          placeholder="请输入一级分类ID"
          v-model="queryParams.categoryFirst"
        />
      </el-form-item>
      <el-form-item label="二级分类ID" prop="categorySecond">
        <el-input
          @keyup.enter.native="handleQuery"
          clearable
          placeholder="请输入二级分类ID"
          v-model="queryParams.categorySecond"
        />
      </el-form-item>
      <el-form-item label="是否上架" prop="shelf">
        <el-input
          @keyup.enter.native="handleQuery"
          clearable
          placeholder="请输入是否上架"
          v-model="queryParams.shelf"
        />
      </el-form-item>
      <el-form-item label="排序字段" prop="sort">
        <el-input
          @keyup.enter.native="handleQuery"
          clearable
          placeholder="请输入排序字段"
          v-model="queryParams.sort"
        />
      </el-form-item>
      <el-form-item label="销售价格" prop="salesPrice">
        <el-input
          @keyup.enter.native="handleQuery"
          clearable
          placeholder="请输入销售价格"
          v-model="queryParams.salesPrice"
        />
      </el-form-item>
      <el-form-item label="市场价" prop="marketPrice">
        <el-input
          @keyup.enter.native="handleQuery"
          clearable
          placeholder="请输入市场价"
          v-model="queryParams.marketPrice"
        />
      </el-form-item>
      <el-form-item label="成本价" prop="costPrice">
        <el-input
          @keyup.enter.native="handleQuery"
          clearable
          placeholder="请输入成本价"
          v-model="queryParams.costPrice"
        />
      </el-form-item>
      <el-form-item label="库存" prop="stock">
        <el-input
          @keyup.enter.native="handleQuery"
          clearable
          placeholder="请输入库存"
          v-model="queryParams.stock"
        />
      </el-form-item>
      <el-form-item label="销量" prop="saleNum">
        <el-input
          @keyup.enter.native="handleQuery"
          clearable
          placeholder="请输入销量"
          v-model="queryParams.saleNum"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" icon="el-icon-search" size="mini" type="primary">搜索</el-button>
        <el-button @click="resetQuery" icon="el-icon-refresh" size="mini">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          @click="handleAdd"
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          v-hasPermi="['weixin:spu:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          :disabled="single"
          @click="handleUpdate"
          icon="el-icon-edit"
          plain
          size="mini"
          type="success"
          v-hasPermi="['weixin:spu:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          :disabled="multiple"
          @click="handleDelete"
          icon="el-icon-delete"
          plain
          size="mini"
          type="danger"
          v-hasPermi="['weixin:spu:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          @click="handleExport"
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          v-hasPermi="['weixin:spu:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :data="spuList" @selection-change="handleSelectionChange" v-loading="loading">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="PK" prop="id"/>
      <el-table-column align="center" label="spu编码" prop="spuCode"/>
      <el-table-column align="center" label="spu名字" prop="name"/>
      <el-table-column align="center" label="卖点" prop="sellPoint"/>
      <el-table-column align="center" label="描述" prop="description"/>
      <el-table-column align="center" label="一级分类ID" prop="categoryFirst"/>
      <el-table-column align="center" label="二级分类ID" prop="categorySecond"/>
      <el-table-column align="center" label="商品图片" prop="picUrls"/>
      <el-table-column align="center" label="是否上架" prop="shelf"/>
      <el-table-column align="center" label="排序字段" prop="sort"/>
      <el-table-column align="center" label="销售价格" prop="salesPrice"/>
      <el-table-column align="center" label="市场价" prop="marketPrice"/>
      <el-table-column align="center" label="成本价" prop="costPrice"/>
      <el-table-column align="center" label="库存" prop="stock"/>
      <el-table-column align="center" label="销量" prop="saleNum"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            @click="handleUpdate(scope.row)"
            icon="el-icon-edit"
            size="mini"
            type="text"
            v-hasPermi="['weixin:spu:edit']"
          >修改
          </el-button>
          <el-button
            @click="handleDelete(scope.row)"
            icon="el-icon-delete"
            size="mini"
            type="text"
            v-hasPermi="['weixin:spu:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
      v-show="total>0"
    />

    <!-- 添加或修改商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      <el-form :model="form" :rules="rules" label-width="80px" ref="form">
        <el-form-item label="spu编码" prop="spuCode">
          <el-input placeholder="请输入spu编码" v-model="form.spuCode"/>
        </el-form-item>
        <el-form-item label="spu名字" prop="name">
          <el-input placeholder="请输入spu名字" v-model="form.name"/>
        </el-form-item>
        <el-form-item label="卖点" prop="sellPoint">
          <el-input placeholder="请输入内容" type="textarea" v-model="form.sellPoint"/>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input placeholder="请输入内容" type="textarea" v-model="form.description"/>
        </el-form-item>
        <el-form-item label="一级分类ID" prop="categoryFirst">
          <el-input placeholder="请输入一级分类ID" v-model="form.categoryFirst"/>
        </el-form-item>
        <el-form-item label="二级分类ID" prop="categorySecond">
          <el-input placeholder="请输入二级分类ID" v-model="form.categorySecond"/>
        </el-form-item>
        <el-form-item label="商品图片" prop="picUrls">
          <el-input placeholder="请输入内容" type="textarea" v-model="form.picUrls"/>
        </el-form-item>
        <el-form-item label="是否上架" prop="shelf">
          <el-input placeholder="请输入是否上架" v-model="form.shelf"/>
        </el-form-item>
        <el-form-item label="排序字段" prop="sort">
          <el-input placeholder="请输入排序字段" v-model="form.sort"/>
        </el-form-item>
        <el-form-item label="销售价格" prop="salesPrice">
          <el-input placeholder="请输入销售价格" v-model="form.salesPrice"/>
        </el-form-item>
        <el-form-item label="市场价" prop="marketPrice">
          <el-input placeholder="请输入市场价" v-model="form.marketPrice"/>
        </el-form-item>
        <el-form-item label="成本价" prop="costPrice">
          <el-input placeholder="请输入成本价" v-model="form.costPrice"/>
        </el-form-item>
        <el-form-item label="库存" prop="stock">
          <el-input placeholder="请输入库存" v-model="form.stock"/>
        </el-form-item>
        <el-form-item label="销量" prop="saleNum">
          <el-input placeholder="请输入销量" v-model="form.saleNum"/>
        </el-form-item>
        <el-form-item label="逻辑删除标记" prop="delFlag">
          <el-input placeholder="请输入逻辑删除标记" v-model="form.delFlag"/>
        </el-form-item>
      </el-form>
      <div class="dialog-footer" slot="footer">
        <el-button @click="submitForm" type="primary">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {addSpu, delSpu, getSpu, listSpu, updateSpu} from "@/api/weixin/spu";

  export default {
    name: "Spu",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 商品表格数据
        spuList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          spuCode: null,
          name: null,
          sellPoint: null,
          description: null,
          categoryFirst: null,
          categorySecond: null,
          picUrls: null,
          shelf: null,
          sort: null,
          salesPrice: null,
          marketPrice: null,
          costPrice: null,
          stock: null,
          saleNum: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [
            {required: true, message: "spu名字不能为空", trigger: "blur"}
          ],
          sellPoint: [
            {required: true, message: "卖点不能为空", trigger: "blur"}
          ],
          description: [
            {required: true, message: "描述不能为空", trigger: "blur"}
          ],
          categoryFirst: [
            {required: true, message: "一级分类ID不能为空", trigger: "blur"}
          ],
          picUrls: [
            {required: true, message: "商品图片不能为空", trigger: "blur"}
          ],
          shelf: [
            {required: true, message: "是否上架不能为空", trigger: "blur"}
          ],
          sort: [
            {required: true, message: "排序字段不能为空", trigger: "blur"}
          ],
          stock: [
            {required: true, message: "库存不能为空", trigger: "blur"}
          ],
          createTime: [
            {required: true, message: "创建时间不能为空", trigger: "blur"}
          ],
          updateTime: [
            {required: true, message: "最后更新时间不能为空", trigger: "blur"}
          ],
          delFlag: [
            {required: true, message: "逻辑删除标记不能为空", trigger: "blur"}
          ]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询商品列表 */
      getList() {
        this.loading = true;
        listSpu(this.queryParams).then(response => {
          this.spuList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          spuCode: null,
          name: null,
          sellPoint: null,
          description: null,
          categoryFirst: null,
          categorySecond: null,
          picUrls: null,
          shelf: null,
          sort: null,
          salesPrice: null,
          marketPrice: null,
          costPrice: null,
          stock: null,
          saleNum: null,
          createTime: null,
          updateTime: null,
          delFlag: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加商品";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getSpu(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改商品";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateSpu(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addSpu(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除商品编号为"' + ids + '"的数据项？').then(function () {
          return delSpu(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('weixin/spu/export', {
          ...this.queryParams
        }, `spu_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
